#pragma once
#include "../logger.hpp"
#include "user_relation.hxx"
#include "user_relation-odb.hxx"
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include <sstream>
#include <string>
#include <vector>

namespace shared {

namespace dal {

class UserRelationDao;
using UserRelationDaoPtr = std::shared_ptr<UserRelationDao>;

class UserRelationDao
{
    using query = odb::query<entity::UserRelation>;
    using result = odb::result<entity::UserRelation>;
public:
    UserRelationDao(const std::shared_ptr<odb::mysql::database>& db) : db_(db) {}

    // 新增指定两用户之间的好友关系
    bool insert(const std::string& uid1, const std::string& uid2)
    {
        try {
            odb::transaction trans(db_->begin());
            entity::UserRelation r1(uid1, uid2);
            entity::UserRelation r2(uid2, uid1);
            db_->persist(r1);
            db_->persist(r2);
            trans.commit();
        } catch (const std::exception& e) {
            LOG_ERROR("向用户关系表新增关系({}-{})失败: {}", uid1, uid2, e.what());
            return false;
        }
        return true;
    }
    // 删除指定两用户之间的好友关系
    bool remove(const std::string& uid1, const std::string& uid2)
    {
        try {
            odb::transaction trans(db_->begin());
            db_->erase_query<entity::UserRelation>(
                (query::userId1 == uid1 && query::userId2 == uid2) || 
                (query::userId1 == uid2 && query::userId2 == uid1)
            );
            trans.commit();
        } catch (const std::exception& e) {
            LOG_ERROR("向用户关系表删除关系({}-{})失败: {}", uid1, uid2, e.what());
            return false;
        }
        return true;
    }
    // 判断指定两用户是否存在好友关系
    bool exists(const std::string& uid1, const std::string& uid2)
    {
        bool ret;
        try {
            odb::transaction trans(db_->begin());
            ret = db_->query<entity::UserRelation>(
                (query::userId1 == uid1 && query::userId2 == uid2) || 
                (query::userId1 == uid2 && query::userId2 == uid1)
            ).size();
            trans.commit();
        } catch (const std::exception& e) {
            LOG_ERROR("向用户关系表删除关系({}-{})失败: {}", uid1, uid2, e.what());
            return false;
        }
        return ret;
    }
    // 获取指定用户的好友ID列表
    std::vector<std::string> selectFriends(const std::string& userId)
    {
        std::vector<std::string> res;
            try {
                odb::transaction trans(db_->begin());
                result ret(db_->query<entity::UserRelation>(query::userId1 == userId));
                for (auto it = ret.begin(); it != ret.end(); ++it) {
                    res.push_back(it->userId2());
                }
                trans.commit(); 
            } catch (const std::exception& e) {
                LOG_ERROR("获取用户({})的好友列表失败: {}", userId, e.what());
        }
        return res;
    }
private:
    std::shared_ptr<odb::mysql::database> db_;
};


}}